define(['amazeui', 'jquery', 'global/url', 'global/constants'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', '$timeout', 'UtilService', 'ItemService', 'AdminService', function($scope, $rootScope, $timeout, UtilService, ItemService, AdminService) {

        var params = {
        	'accessToken': $scope.accessToken,
        	'filter': {
	        	'pagesize': 10,
	        	'pno': 1,
	        	'search': ''
	        }
        }

        $scope.searchItems = function (event) {
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	_getItemList(params);
        } 

        $scope.forwardPage = function (event) {
        	var pno = event.target.text;
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = pno;
        	_getItemList(params);
        }

        $scope.prePage = function () {
        	var currentPage = $scope.items.currentPage;
        	if (currentPage === 1) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = currentPage - 1;
        	_getItemList(params);
        }

        $scope.nextPage = function () {
        	var currentPage = $scope.items.currentPage;
        	if (currentPage === $scope.items.totalPagers) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = currentPage + 1;
        	_getItemList(params);
        }

        $scope.fileReaderSupported = window.FileReader != null && (window.FileAPI == null || FileAPI.html5 != false);
        $scope.generateThumb = function(file) {
            if (file != null) {
                if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
                    $timeout(function() {
                        var fileReader = new FileReader();
                        fileReader.readAsDataURL(file);
                        fileReader.onload = function(e) {
                            $timeout(function() {
                                file.dataUrl = e.target.result;
                                UtilService.uploadFile(file, $scope.accessToken).success(function(response, status){
                                    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                                        $scope.item.icon = response.jsonResponse.response.meta.message;                                    }
                                });
                            });
                        }
                    });
                }
            }
        }


	    function _getItemList (params) {
	    	
	    	ItemService.listItems(params).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.items = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	           AdminService.Admin.logout();
	  	           window.location.href = constants.pages.login;
	  	        }
            });
	    }


	   UtilService.renderSelect();
	   _getItemList(params);

    }];
  }
)
